//
//  BHAlertView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/25.
//

import UIKit

class BHAlertView: UIView {

    var cancelActBlock:(() -> Void)?
    var confirmActBlock:(() -> Void)?
    
    
    @IBOutlet weak var content_view: UIView!
    
    @IBOutlet weak var title_label: UILabel!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    @IBOutlet weak var confirmBtn: UIButton!
    
    @IBOutlet weak var text_textView: UITextView!
    
    
    static func loadFromXib() -> BHAlertView {
        let viewArray = Bundle.main.loadNibNamed("BHAlertView", owner: nil, options: nil)
        let view = viewArray![0] as! BHAlertView
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.height)
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.backgroundColor = UIColor.black.withAlphaComponent(0.4)
        self.content_view.addLayerCorner(10, 0, .white)
        self.cancelBtn.addLayerCorner(19, 0, .white)
        self.confirmBtn.addLayerCorner(19, 0, .white)
        
        self.content_view.transform = CGAffineTransform.init(scaleX: 0.1, y: 0.1)
    }
    
    func show()  {
        UIView.animate(withDuration: 0.25) {
            self.content_view.transform = .identity
        }
    }
    
    func dissMiss() {
        
        UIView.animate(withDuration: 0.25) {
            self.content_view.transform = CGAffineTransform.init(scaleX: 0.1, y: 0.1)
        } completion: { (finished) in
            if finished == true {
                self.removeFromSuperview()
            }
        }
    }
    
    @IBAction func cancelBtnAct(_ sender: UIButton) {
        self.cancelActBlock?()
        self.dissMiss()
    }
    
    
    @IBAction func confirmAct(_ sender: UIButton) {
        self.confirmActBlock?()
        self.dissMiss()
    }
}
